//
//  SCPlacehodlerView.m
//  Shuaiche
//
//  Created by 党帅 on 2019/6/12.
//  Copyright © 2019 zzs. All rights reserved.
//

#import "SCPlacehodlerView.h"

@interface SCPlacehodlerView ()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *tipLabel;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation SCPlacehodlerView

- (instancetype)init {
    self = [super init];
    if (self) {
        self.axis           = UILayoutConstraintAxisVertical;
        self.alignment      = UIStackViewAlignmentCenter;
//        self.distribution   = UIStackViewDistributionFillProportionally;
        self.spacing        = 15;
    }
    return self;
}

- (void)buttonClick {
    if (self.eventInvocation) {
        [self.eventInvocation invoke];
    }
}

- (void)setImageName:(NSString *)imageName {
    if ([_imageName isEqualToString:imageName]) {
        return;
    }
    _imageName = imageName;
    self.imageView.image = [UIImage imageNamed:_imageName];
}

- (void)setMessage:(NSString *)message {
    if ([message isEqualToString:_message]) {
        return;
    }
    _message = message;
    self.tipLabel.text = _message;
}

- (void)setTitle:(NSString *)title {
    if (title) {
        _title = title;
        self.titleLabel.text = title;
    }
}

- (void)setButtonTitle:(NSString *)buttonTitle {
    _buttonTitle = buttonTitle;
    if (buttonTitle) {
        [self.button setTitle:_buttonTitle forState:0];
        _button.hidden = NO;
    } else {
        _button.hidden = YES;
    }
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        [self addSubview:_imageView];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        [self addArrangedSubview:_imageView];
    }
    return _imageView;
}

- (UILabel *)tipLabel {
    if (!_tipLabel) {
        _tipLabel = [[UILabel alloc] init];
        _tipLabel.font = SC_FONT(15);
        _tipLabel.textColor = SC_TextBlack_Secondary;
        _tipLabel.textAlignment = 1;
        _tipLabel.numberOfLines = 0;
        [self addArrangedSubview:_tipLabel];
    }
    return _tipLabel;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = SC_FONT_Meduim(15);
        _titleLabel.textColor = SC_TextBlack_Primary;
        _titleLabel.textAlignment = 1;
        _titleLabel.numberOfLines = 0;
        [self addArrangedSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UIButton *)button {
    if (!_button) {
        
        // 文本与按钮之间距离
        UIView *view = [[UIView alloc] init];
        view.translatesAutoresizingMaskIntoConstraints = NO;
        [view.heightAnchor constraintEqualToConstant:1].active = YES;
        [self addArrangedSubview:view];
        
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.titleLabel.font = SC_FONT(15);
        _button.layer.cornerRadius = 4;
        _button.layer.masksToBounds = YES;
        [_button setTitleColor:SC_TextBlack_Primary forState:0];
        [_button setBackgroundColor:SC_Color_Tint];
        [_button setContentEdgeInsets:UIEdgeInsetsMake(12, 25, 12, 25)];
        [_button.widthAnchor constraintGreaterThanOrEqualToConstant:140].active = YES;;
        [_button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
        [self addArrangedSubview:_button];
    }
    return _button;
}

@end
